﻿
Imports System.Data.SqlClient
Public Class DueñoDAO
    Public Function insertar(ByVal modelo As Dueño) As Dueño

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "INSERT INTO DUEÑO (NOMBRES,APELLIDOP,APELLIDOM,CORREO,CLAVE,TELEFONO,DIRECCION,DNI) VALUES (@VALOR2, @VALOR3, @VALOR4, @VALOR5, @VALOR6, @VALOR7, @VALOR8, @VALOR9)"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR2", modelo.Nombres)
            cmd.Parameters.AddWithValue("@VALOR3", modelo.ApellidoP)
            cmd.Parameters.AddWithValue("@VALOR4", modelo.ApellidoM)
            cmd.Parameters.AddWithValue("@VALOR5", modelo.Correo)
            cmd.Parameters.AddWithValue("@VALOR6", modelo.Clave)
            cmd.Parameters.AddWithValue("@VALOR7", modelo.Telefono)
            cmd.Parameters.AddWithValue("@VALOR8", modelo.Direccion)
            cmd.Parameters.AddWithValue("@VALOR9", modelo.DNI)

            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("DueñoDAO: No insertó")
            End If

            ' LEER EL ULTIMO ID
            sql = "SELECT @@IDENTITY"
            cmd = New SqlCommand(sql, cn)
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                'modelo.idDueño = CType(dr(0), Integer)
            End If

            dr.Close()
            cn.Close()

        Catch ex As Exception
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return modelo

    End Function


    Public Function buscarPorNombre(ByVal apellidoP As String) As Collection
        Dim lista As New Collection

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT IDDUEÑO,NOMBRES,APELLIDOP,APELLIDOM,CORREO,CLAVE,TELEFONO,DIRECCION,DNI FROM DUEÑO WHERE UPPER(ApellidoP) LIKE @VALOR1"

            Dim cmd As New SqlCommand(sql, cn)

            cmd.Parameters.AddWithValue("@VALOR1", "%" + "%" + apellidoP.ToUpper + "%" + "%" + "%" + "%" + "%" + "%")
            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()

            While dr.Read
                Dim modelo As New Dueño

                modelo.idDueño = dr(0).ToString
                modelo.Nombres = dr(1).ToString
                modelo.ApellidoP = dr(2).ToString
                modelo.ApellidoM = dr(3).ToString
                modelo.Correo = dr(4).ToString
                modelo.Clave = dr(5).ToString
                modelo.Telefono = dr(6).ToString
                modelo.Direccion = dr(7).ToString
                modelo.DNI = dr(8).ToString
             

                lista.Add(modelo)
            End While

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return lista
    End Function


    Public Function obtener(ByVal apellidoP As Integer) As Dueño
        Dim modelo As New Dueño

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT IDDUEÑO,NOMBRES,APELLIDOP,APELLIDOM,CORREO,CLAVE,TELEFONO,DIRECCION,DNI FROM DUEÑO WHERE UPPER(APELLIDOP) LIKE @VALOR1"
            Dim cmd As New SqlCommand(sql, cn)

            cmd.Parameters.AddWithValue("@VALOR1", apellidoP)

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            If dr.Read Then
                modelo.idDueño = dr(0).ToString
                modelo.Nombres = dr(1).ToString
                modelo.ApellidoP = dr(2).ToString
                modelo.ApellidoM = dr(3).ToString
                modelo.Correo = dr(4).ToString
                modelo.Clave = dr(5).ToString
                modelo.Telefono = dr(6).ToString
                modelo.Direccion = dr(7).ToString
                modelo.DNI = dr(8).ToString
            End If

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return modelo
    End Function

    Public Sub actualizar(ByVal modelo As Dueño)
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "UPDATE ESTUDIANTE SET idDueño=@VALOR1, Nombres=@VALOR2, ApellidoP=@VALOR3, ApellidoM=@VALOR4, Correo=@VALOR5, Clave=@VALOR6, Telefono=@VALOR7, Direccion=@VALOR8, DNI=@VALOR9 WHERE idDueño=@VALOR1"

            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", modelo.idDueño)
            cmd.Parameters.AddWithValue("@VALOR2", modelo.Nombres)
            cmd.Parameters.AddWithValue("@VALOR3", modelo.ApellidoP)
            cmd.Parameters.AddWithValue("@VALOR4", modelo.ApellidoM)
            cmd.Parameters.AddWithValue("@VALOR8", modelo.Correo)
            cmd.Parameters.AddWithValue("@VALOR11", modelo.Clave)
            cmd.Parameters.AddWithValue("@VALOR7", modelo.Telefono)
            cmd.Parameters.AddWithValue("@VALOR6", modelo.Direccion)
            cmd.Parameters.AddWithValue("@VALOR10", modelo.DNI)


            Dim afectados = cmd.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("DueñoDAO: No actualizó")
            End If

            cn.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
            Throw New DAOExcepcion(ex.ToString)
        End Try
    End Sub

End Class
